package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.card.EntityXfRecordRepairpay;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.repairpay.GetRepairPayRecordListRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfRecordRepairpay;
import com.ocom.payconfig.service.XfRecordRepairpayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfRecordRepairpayServiceImpl extends ServiceImpl<IDaoXfRecordRepairpay, EntityXfRecordRepairpay> implements XfRecordRepairpayService {

    @Autowired
    private IDaoXfRecordRepairpay iDaoXfRecordRepairpay;

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public PageInfo<EntityXfRecordRepairpay> getXfRecordRepairpay(Long comId, GetRepairPayRecordListRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());

        QueryWrapper<EntityXfRecordRepairpay> queryWrapper = Wrappers.query();

        queryWrapper.eq("com_id", comId);

        if (null != request.getMerchId() && -1 != request.getMerchId()) {
            queryWrapper.eq("merch_id", request.getMerchId());
        }
        if (null != request.getPointId() && -1 != request.getPointId()) {
            queryWrapper.eq("point_id", request.getPointId());
        }
        if (StringUtils.isNotBlank(request.getDeviceNo())) {
            queryWrapper.eq("device_no", request.getDeviceNo());
        }
        if (null != request.getStartTime()) {
            queryWrapper.ge("start_time", request.getStartTime());
        }
        if (null != request.getEndTime()) {
            queryWrapper.le("end_time", request.getEndTime());
        }
        if (StringUtils.isNotBlank(request.getPName())) {
            queryWrapper.eq("p_name", request.getPName());
        }
        if (StringUtils.isNotBlank(request.getAccountNo())) {
            queryWrapper.eq("account_no", request.getAccountNo());
        }
        if (StringUtils.isNotBlank(request.getPCode())) {
            queryWrapper.eq("p_code", request.getPCode());
        }
        if (null != request.getPId()) {
            queryWrapper.eq("p_id", request.getPId());
        }
        queryWrapper.orderByDesc("cons_time");

        List<EntityXfRecordRepairpay> list = this.list(queryWrapper);
        return new PageInfo<>(list);
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean save(Long comId, EntityXfRecordRepairpay entity) {
        return super.save(entity);
    }
}
